// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.notifications;

import java.util.Date;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.notifications.AbstractNotification;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.connectivity.IConnectivityProperty;
import org.dengues.ui.prefs.connectivity.ConnectivitySettingTreeProvider;
import org.dengues.warehouse.viewers.action.OpenPrefsSettingsAction;
import org.eclipse.jface.viewers.DecoratingLabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-6 qiang.zhang $
 * 
 */
public class ConnPropertyNotification extends AbstractNotification {

    protected final IConnectivityProperty connectivityProperty;

    protected Date date;

    private String description = null;

    private final DecoratingLabelProvider labelProvider = new DecoratingLabelProvider(new ConnectivitySettingTreeProvider(),
            PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator());

    private boolean isSuccessful;

    public ConnPropertyNotification(IConnectivityProperty connectivityProperty) {
        this.connectivityProperty = connectivityProperty;
    }

    @Override
    public String getDescription() {
        if (description == null) {
            return connectivityProperty.getStatusMessage();
        }
        return description;
    }

    @Override
    public String getLabel() {
        return labelProvider.getText(connectivityProperty);
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public void open() {

        PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

            public void run() {
                new OpenPrefsSettingsAction().run();
            }
        });
    }

    @Override
    public Image getNotificationImage() {
        return labelProvider.getImage(connectivityProperty);
    }

    @Override
    public Image getNotificationKindImage() {
        if (connectivityProperty.isValid()) {
            if (isSuccessful) {
                return ImageUtil.getImage(IDenguesSharedImage.POPUP_NOTIFICATION_OK);
            } else {
                return ImageUtil.getImage(IDenguesSharedImage.POPUP_NOTIFICATION_MISTAKE);
            }
        }
        return ImageUtil.getImage(IDenguesSharedImage.POPUP_NOTIFICATION_ERROR);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.notifications.AbstractNotification#getDate()
     */
    @Override
    public Date getDate() {
        return date;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.notifications.AbstractNotification#setDate(java.util.Date)
     */
    @Override
    public void setDate(Date date) {
        this.date = date;
    }

    public int compareTo(AbstractNotification anotherNotification) throws ClassCastException {
        if (!(anotherNotification != null))
            throw new ClassCastException("A AbstractNotification object expected.");
        Date anotherDate = (anotherNotification).getDate();
        if (date != null && anotherDate != null) {
            return date.compareTo(anotherDate);
        } else if (date == null) {
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((date == null) ? 0 : date.hashCode());
        result = prime * result + ((description == null) ? 0 : description.hashCode());
        result = prime * result + ((connectivityProperty == null) ? 0 : connectivityProperty.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ConnPropertyNotification other = (ConnPropertyNotification) obj;
        if (date == null) {
            if (other.date != null)
                return false;
        } else if (!date.equals(other.date))
            return false;
        if (description == null) {
            if (other.description != null)
                return false;
        } else if (!description.equals(other.description))
            return false;
        if (connectivityProperty == null) {
            if (other.connectivityProperty != null)
                return false;
        } else if (!connectivityProperty.equals(other.connectivityProperty))
            return false;
        return true;
    }

    public Object getAdapter(Class adapter) {
        if (adapter == IConnectivityProperty.class) {
            return connectivityProperty;
        }
        return null;
    }

    public void setSuccessful(boolean isSuccessful) {
        this.isSuccessful = isSuccessful;
    }

}
